-- mazz.lua

-- By Tyrone, on 22/04/07
-- Mazz ladder

-- set up all command prefixes
cmdNames = {
    "mazz"
}

mazzAdmins = {
	VoidBoi = true;
	Heinko = true;
	Aethereal = true;
	Pace = true;
}


COOLDOWN = 120 -- plugin cooldown, 0 for none
bot_name = "Pace"
bot_desc = "bot description here"
bot_email = "bot@nowhere.com"
use_bot = false
nl = string.format('\n')

function doCommand(nick, data, z)
    if (data == "ladder") then
		if (ready(nick) == false) then
			mazzRankings = ""
			table.sort(mazzLadder)
			positions = {}
			sizeofpositions = 0
			for name,score in pairs(mazzLadder) do
				table.insert(positions, name)
				sizeofpositions = sizeofpositions + 1
			end
			g = ""
			for i = 1, sizeofpositions, 1 do
				for j = 1, sizeofpositions, 1 do
					if (mazzLadder[positions[i]] > mazzLadder[positions[j]]) then
						g = positions[i]
						positions[i] = positions[j]
						positions[j] = g
					end
				end
			end
			if (sizeofpositions > 5) then sizeofpositions = 5 end
			for i = 1, sizeofpositions, 1 do
				mazzRankings = mazzRankings..positions[i]..": "..mazzLadder[positions[i]]..nl
			end

			if (mazzRankings == "") then
				SendMessageToHub("OMG the mazz ladder is empty!  Quick, someone mazz!!!", bot_name)
			else
				SendMessageToHub("Current Top 5 Mazz Ladder Rankings:"..nl..mazzRankings, bot_name)
			end				
		end
	elseif (data == "up") or (data == "finish") or (data == "end") or (data == "done") then
		if (ready(nick) == false) then
			_,sIP = VH:GetUserIP(nick)
			if (banned[sIP] == 1) then
				SendMessageToUser("Tard, you can't mazz up because you've been mazzbanned.  Suck it.", nick, bot_name)
			else
				if (mazzTimer[nick] == nil) or (mazzTimer[nick] == 0) then
					SendMessageToUser("You can't mazz up until you start mazzing with the .mazz start command, tard.  For your dumbassedness, you have to wait the mazz cooldown to try again.", nick, bot_name)
				else
					difference = mazzRandomTime[nick] - mazzTimer[nick]
					if (difference > 15) then  --legit :D
						if (mazzLadder[nick] == nil) then
							mazzLadder[nick] = 1
						else
							mazzLadder[nick] = mazzLadder[nick] + 1
						end
						SendMessageToHub("Wowzers, "..nick.." completed their mazz in "..difference.."s out of their "..mazzRandomTime[nick].."s bringing them up to "..mazzLadder[nick].." mazzes!", bot_name)
						mazzTimer[nick] = 0
						mazzRandomTime[nick] = 0
					else
						SendMessageToHub("Sigh, clearly "..nick.." is fucking lying about their mazz count.  They have been reset to zero.", bot_name)
						mazzLadder[nick] = 0
					end
				end
			end
		end
	elseif (string.find(data, "^down")) then
		if (UserClass(nick) == 10) or (mazzAdmins[nick] ~= nil) then
			_,_,nick2 = string.find(data, "^down%s(.+)")
			if (mazzLadder[nick2] == nil) then
				SendMessageToUser("This user is not on the mazz ladder!", nick, bot_name)
			elseif (mazzLadder[nick2] == 0) then
				SendMessageToUser("Negative mazz's isn't possible...", nick, bot_name)
			else
				SendMessageToHub("Apparently "..nick2.." is a fucking liar!  An admin has decreased their mazz count! :O", bot_name)
				mazzLadder[nick2] = mazzLadder[nick2] - 1
				if (mazzLadder[nick2] == 0) then mazzLadder[nick2] = nil end
			end
		else
			SendMessageToUser("Only admins can perform this command.  If you need to un-mazz, let an admin know.", nick, bot_name)
		end
	elseif (string.find(data, "^ban")) then
		if (UserClass(nick) == 10) or (mazzAdmins[nick] ~= nil) then
			_,_,nick2 = string.find(data, "^ban%s(.+)")
			if (UserIsOnline(nick2)) then
				_,sIP = VH:GetUserIP(nick2)
				if (banned[sIP] == 1) then
					SendMessageToUser("This user is already mazzbanned.", nick, bot_name)
				else
					banned[sIP] = 1
					SendMessageToHub(nick2.." is a massive faggot and was just mazzbanned by "..nick..".  No more mazz for you.", bot_name)
				end
			else
				SendMessageToUser("User is not online - cannot ban them without them being online since I can't retrieve their IP address.", nick, bot_name)
			end
		else
			SendMessageToUser("You are not an administrator.", nick, bot_name)
		end
	elseif (string.find(data, "^bunan")) then
		if (UserClass(nick) == 10) or (mazzAdmins[nick] ~= nil) then
			_,_,nick2 = string.find(data, "^bunan%s(.+)")
			if (UserIsOnline(nick2)) then
				_,sIP = VH:GetUserIP(nick2)
				if (banned[sIP] == 0) then
					SendMessageToUser("This user is already mazzbunanned.", nick, bot_name)
				else
					banned[sIP] = 0
					SendMessageToHub("Mazzbunanned "..nick2.."...", bot_name)
				end
			else
				SendMessageToUser("User is not online - cannot bunan them without them being online since I can't retrieve their IP address.", nick, bot_name)
			end
		else
			SendMessageToUser("You are not an administrator.", nick, bot_name)
		end
	elseif (string.find(data, "^set")) then
		if (UserClass(nick) == 10) or (mazzAdmins[nick] ~= nil) then	
			_,_,instr = string.find(data, "^set%s(.+)")
			_,_,nick2 = string.find(instr, "^(.+)%s")
			_,_,val = string.find(instr, "%s(%d+)")
			if (val == nil) then
				SendMessageToUser("Invalid mazz count.  Nick="..nick2, nick, bot_name)
			else
			val = tonumber(val)
				if (val > 0) then
					mazzLadder[nick2] = val
					SendMessageToHub("The user "..nick2.." just got their mazz count set to "..val..".", bot_name)
				else
					mazzLadder[nick2] = nil
					SendMessageToHub("The user "..nick2.." was removed from the mazz ladder.", bot_name)
				end
			end
		else
			SendMessageToUser("You are not an administrator.", nick, bot_name)
		end
	elseif (string.find(data, "^start")) then
		_,sIP = VH:GetUserIP(nick)
		if (banned[sIP] == 1) then
			SendMessageToUser("Tard, you can't mazz start because you've been mazzbanned.  Suck it.", nick, bot_name)
		else
			if (mazzTimer[nick] == nil) or (mazzTimer[nick] == 0) then
				randomTime = math.floor(math.random(60,120))
				randomTime = tonumber(randomTime)
				SendMessageToUser("You've started mazzing!  Quick GOGOGOGO, you have "..randomTime.."s left to mazz!  :O", nick, bot_name)
				mazzTimer[nick] = randomTime
				mazzRandomTime[nick] = randomTime
			else
				SendMessageToUser("Stop wasting time trying restart your timer!  You only have "..mazzTimer[nick].."s left to mazz!  Chop chop!", nick, bot_name)
			end
		end
	else
		doHelp(nick)
	end
end

function doHelp(nick)
    SendMessageToUser("Usage: ", nick, bot_name)
    DisplayHelpMessage("help", "Shows this message.", nick)
    -- put coad in here for help commands:
    DisplayHelpMessage("ladder", "Displays the current mazz rankings.  Can only be used every 120 seconds.", nick)
	DisplayHelpMessage("up", "Increments your mazz count.  Can only be used after you've started mazzing with the start command.", nick)
	DisplayHelpMessage("start", "Starts your mazz session.  You will be allocated between 60 and 120 seconds to mazz.  Use the up command to signal when you've completed your mazz.", nick)
end

-- insert other functions here
mazzLadder = {}
banned = {}
mazzTimer = {}
mazzRandomTime = {}
---------------------------------------
-- you can ignore below here
timeLeft = {} 

function Main()
    -- Check if script should add a new bot.
    if (use_bot) then
    -- Add new bot.
        VH:AddRobot(bot_name, 10, bot_desc, "Bot ", bot_email, "0")
    end
end

function UnLoad()
	if (use_bot) then
    	VH:DelRobot(bot_name)
	end
end

function VH_OnParsedMsgChat(nick, data)
    for i=1, table.maxn(cmdNames), 1 do
		if (string.find(data, "^%."..cmdNames[i])) then
		    _,_,cmd = string.find(data, "^%."..cmdNames[i].."%s(.+)")
		    if (cmd == "help") then
		        doHelp(nick)
		    else
        		doCommand(nick, cmd, cmdNames[i])
        	end
			return 0
    	end
	end
	return 1
end

function ready(nick)
    if (timeLeft[nick] == 0) then
		timeLeft[nick] = COOLDOWN
	elseif (timeLeft[nick] == nil) then
		timeLeft[nick] = COOLDOWN
	else
		SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  You have "..timeLeft[nick].." seconds to wait.", nick, bot_name)
		return true
	end
	return false
end

function SendMessageToHub(data, from)
	return VH:SendDataToAll("<"..from.."> "..data.."|",0,10)
end

function SendMessageToUser(data, nick, from)
	return VH:SendDataToUser("<"..from.."> "..data.."|", nick)
end

function SendPrivateMessageToUser(user, data, from)
    return VH:SendDataToUser("$To: "..user.." From: "..from.." $<"..from.."> "..data.."|", user);
end

function DisplayHelpMessage(cmd, msg, nick)
    SendMessageToUser("        ."..cmdNames[1].." "..cmd, nick, bot_name)
    SendMessageToUser("        "..msg, nick, bot_name)
end

function UserClass(nick)
	res, class = VH:GetUserClass(nick)
	if res and class then return class else return 0 end
end

function UserIsOnline(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return false
	else
		return true
	end
end

function VH_OnTimer()
	for nick,time in pairs(timeLeft) do
		if (time > 0) then
			timeLeft[nick] = timeLeft[nick] - 1
		end
	end
	
	for nick,time in pairs(mazzTimer) do
		if (time > 0) then
			mazzTimer[nick] = mazzTimer[nick] - 1
			if (mazzTimer[nick] == 0) then
				SendMessageToHub("Oh noes, "..nick.." has failed to mazz in the allocated time!  They lose a mazz point!", bot_name)
				if (mazzLadder[nick] == nil) then
					mazzLadder[nick] = 0
				end
				mazzLadder[nick] = mazzLadder[nick] - 1
			end
		end
	end		
	return 1
end